/**
 * $Id: EditorImageBundle.java,v 1.5 2011-01-19 20:06:16 gaudenz Exp $
 * Copyright (c) 2010, Gaudenz Alder
 */
package com.kg.rails.component.editor.util;

import java.util.Hashtable;
import java.util.Map;

/**
 * Maps from keys to base64 encoded images or file locations. All values must
 * be URLs or use the format data:image/format followed by a comma and the base64
 * encoded image data, eg. "data:image/gif,XYZ", where XYZ is the base64 encoded
 * image data.
 * <p/>
 * To add a new image bundle to an existing graph, the following code is used:
 * <p/>
 * <code>
 * EditorImageBundle bundle = new EditorImageBundle();
 * bundle.PutImage("myImage", "data:image/gif,R0lGODlhEAAQAMIGAAAAAICAAICAgP" +
 * "//AOzp2O3r2////////yH+FUNyZWF0ZWQgd2l0aCBUaGUgR0lNUAAh+QQBCgAHACwAAAAA" +
 * "EAAQAAADTXi63AowynnAMDfjPUDlnAAJhmeBFxAEloliKltWmiYCQvfVr6lBPB1ggxN1hi" +
 * "laSSASFQpIV5HJBDyHpqK2ejVRm2AAgZCdmCGO9CIBADs=");
 * graph.addImageBundle(bundle);
 * </code>
 * <p/>
 * The image can then be referenced in any cell style using image=myImage.
 * If you are using mxOutline, you should use the same image bundles in the
 * graph that renders the outline.
 * <p/>
 * To convert a given BufferedImage to a base64 encoded String, the following
 * code can be used:
 * <p/>
 * <code>
 * ByteArrayOutputStream bos = new ByteArrayOutputStream();
 * ImageIO.write(image, "png", bos);
 * System.out.println("base64=" + Base64.encodeToString(
 * bos.toByteArray(), false));
 * </code>
 * <p/>
 * The value is decoded in EditorUtils.loadImage. The keys for images are resolved
 * and the short format above is converted to a data URI in
 * Graph.postProcessCellStyle.
 */
public class EditorImageBundle {

    /**
     * Maps from keys to images.
     */
    protected Map<String, String> images = new Hashtable<String, String>();

    /**
     * Returns the images.
     */
    public Map<String, String> getImages() {
        return images;
    }

    /**
     * Adds the specified entry to the map.
     */
    public void putImage(String key, String value) {
        images.put(key, value);
    }

    /**
     * Returns the value for the given key.
     */
    public String getImage(String key) {
        if (key != null) {
            return images.get(key);
        }

        return null;
    }

}
